<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class AuthController extends Controller
{
    public function login(Request $request){
        $validate = $this->validate($request,[
            'email'=>'required',
            'password'=>'required'
        ]);
//        dump(Hash::make($validate['password']));


        $user = User::where('email',$validate['email'])->first();
        if (empty($user) || !Hash::check($validate['password'],$user->password)) return $this->error_401('user credentials are invalid');

        $token = uniqid($user->id);
        $user->api_token = $token;
        $user->save();
        $user->token = $user->api_token;
        $user->makeHidden(['photo','firstname','lastname']);
        return $this->success($user);
    }
    public function logout(){
        $user = auth()->user();
        $user->api_token = null;
        $user->save();

        return $this->success();
    }
}
